<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	 <script src="qczlm/js/jquery.min.js"></script>
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html" />
    <![endif]-->
    <script>
        if (window.top !== window.self) {
            window.top.location = window.location;
        }
        var countdown=5; 
		function settime(obj) { 
		    if (countdown == 0) { 
		        obj.removeAttribute("disabled");    
		        obj.value="免费获取验证码"; 
		        countdown = 5; 
		        return;
		    } else { 
		        obj.setAttribute("disabled", true); 
		        obj.value="重新发送(" + countdown + ")"; 
		        countdown--; 
		    } 
		setTimeout(function() { 
		    settime(obj) }
		    ,1000) 
		}
  
    </script>
  </head>
  
  <body style="text-align: center;">
		<h4 class="no-margins">注册：</h4>
		<p class="m-t-md">注册到千城智联云平台</p>
		<input type="text" id="appname" placeholder="分平台名称"/><div id="appnameText"></div><br />
		<hr/>
		<input type="text" id="username" placeholder="用户名称"/><br />
		<hr/>
		<input type="text" id="phone" placeholder="手机号码"/><br />
		<hr/>
		<input type="text" id="code" placeholder="验证码" /><div id="codeText"></div><br />
		<input type="password" id="new_pd" placeholder="输入密码"><br />
		<input type="password" id="old_pd" placeholder="确认密码"><br /><hr />
		<input type="button" id="gcode" value="免费获取验证码" /> 
		<button id="register" >注册</button>
		<div id="success"></div>
	<!-- js代码 -->
	<script type="text/javascript">
		$(function(){
			//获取验证码
			$("#gcode").click(function(){
				var phone=$("#phone").val();
				if(!phone.replace(/\s/g,"")){
					alert("手机号码不能为空"+phone);
				}else{
					settime(this);
					$.ajax({
						url:'mexamine/send',
					    type:'get',
					    data:{phone:phone},
					    dataType:'json',
					    success:function(JR){
					    	JR=eval("("+JR+")");
					    	if(JR.success=="true"){
					    	}
					    }
					});
				}
			});
			
			//检测当前平台是否已经注册
			$("#appname").bind("input propertychange", function(){
				var appname=$("#appname").val();
				if(appname.replace(/\s/g,"")){
					$.ajax({
						url:'branch/checkbranch',
						type:'get',
						data:{appname:appname},
						dataType:'json',
						success:function(JR){
							document.getElementById("appnameText").innerHTML=""+JR.msg;
						}
					});
				}else{
					$("#appnameText").html("");
				}
			});
			//注册
			$("#register").click(function(){
				var code=$("#code").val();		//验证码
				var phone=$("#phone").val();	//手机号码
				var appname=$("#appname").val();//分平台名
				var username=$("#username").val();//用户名
				var new_pd=$("#new_pd").val();
				var old_pd=$("#old_pd").val();
				if(!code.replace(/\s/g,"")){
					alert("验证码不能为空");
				}else if(!phone.replace(/\s/g,"")){
					alert("手机号码不能为空");
				}else if(!appname.replace(/\s/g,"")){
					alert("分平台名称不能为空");
				}else if(!username.replace(/\s/g,"")){
					alert("用户名不能为空");
				}else if(!new_pd.replace(/\s/g,"") && !old_pd.replace(/\s/g,"")){
					alert("密码不能为空");
				}else{
					if(new_pd!=old_pd){
						alert("请确认密码是否一致！");
						return;
					}
					checkcode(phone,code,appname,username,new_pd);
				}
			});
		});
		
		//注册方法
		function checkcode(phone,code,appname,username,new_pd){
			$.ajax({
				url:'mexamine/checkcode',
			    type:'post',
			    data:{
			        phone:phone,
			        code:code
			    },
			    dataType:'json',
			    success:function(JR){
			    	if(JR.success==true){
			    		$("#codeText").html("");
			    		$.ajax({
							url:'branch/register',
						    type:'post',
						    data:{
						    	appname:$("#appname").val(),
						    	username:$("#username").val(),
						        phone:phone,
						        password:new_pd
						    },
						    dataType:'json',
						    success:function(JR){
						    	if(JR.success==true){
						    		$("#success").html("注册成功,secret为："+JR.obj["secret"]);
						    		window.location.href="mexamine/success?secret="+JR.obj["secret"]+"&createTime="+JR.obj["createTime"]
						    		+"&id="+JR.obj["id"]+"&appname="+appname;
						    	}else{
						    		$("#success").html("注册失败");
						    	}
						    }
						});
			    	}else{
			    		$("#success").html("");
			    		$("#codeText").html("验证码错误");
			    	}
			    }
			});
		}
	</script>
</body>
</html>
